package todo;

import se.lth.cs.realtime.RTThread;
import done.AbstractWashingMachine;

public class WashingProgram1 extends WashingProgram {

	protected WashingProgram1(AbstractWashingMachine mach, double speed,
			TemperatureController tempController,
			WaterController waterController, SpinController spinController) {
		super(mach, speed, tempController, waterController, spinController);
	}

	@Override
	protected void wash() throws InterruptedException {
		
		System.out.println("Starting Washing program 1");
		myMachine.setLock(true);
		
		myWaterController.putEvent(new WaterEvent(this, WaterEvent.WATER_FILL, 0.5));
		mailbox.doFetch();
		myWaterController.putEvent(new WaterEvent(this, WaterEvent.WATER_IDLE, 0.5));
		myTempController.putEvent(new TemperatureEvent(this, TemperatureEvent.TEMP_SET, 60));
		mailbox.doFetch();
		mySpinController.putEvent(new SpinEvent(this, SpinEvent.SPIN_SLOW));
		
		RTThread.sleep((long) (30*60*1000/mySpeed));
		
		mySpinController.putEvent(new SpinEvent(this, SpinEvent.SPIN_OFF));
		myTempController.putEvent(new TemperatureEvent(this, TemperatureEvent.TEMP_IDLE, 60));
		
		myWaterController.putEvent(new WaterEvent(this, WaterEvent.WATER_DRAIN, 0.0));
		mailbox.doFetch();
		
		// Rinse 5 times
		
		for (int i = 0 ; i < 5 ; i++){
			mySpinController.putEvent(new SpinEvent(this, SpinEvent.SPIN_SLOW));
			myWaterController.putEvent(new WaterEvent(this, WaterEvent.WATER_FILL, 0.5));
			mailbox.doFetch();
			myWaterController.putEvent(new WaterEvent(this, WaterEvent.WATER_IDLE, 0.5));
			RTThread.sleep((long) (2*60*1000/mySpeed));
			myWaterController.putEvent(new WaterEvent(this, WaterEvent.WATER_DRAIN, 0.0));
			mailbox.doFetch();
			myWaterController.putEvent(new WaterEvent(this, WaterEvent.WATER_IDLE, 0.5));
			mySpinController.putEvent(new SpinEvent(this, SpinEvent.SPIN_OFF));
		}
		mySpinController.putEvent(new SpinEvent(this, SpinEvent.SPIN_FAST));
		RTThread.sleep((long) (5*60*1000/mySpeed));
		mySpinController.putEvent(new SpinEvent(this, SpinEvent.SPIN_OFF));
		
		myMachine.setLock(false);
	}

}
